package com.appcenter.application.mapper;

import com.appcenter.application.dto.RolePermissionDto;
import com.appcenter.application.input.RolePermission.GetRolePermissionsInput;
import com.appcenter.application.model.Apps;
import com.appcenter.application.model.RolePermissions;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface RolePermissionsMapper extends Mapper<RolePermissions> {

    @Select("<script>" +
            "select b.Id,#{RoleId} as RoleId,a.Id as PermissionId,a.PermissionName,a.Level,CASE WHEN b.Id is NULL THEN 0 ELSE 1 END isChecked from permissions a " +
            " left JOIN (select * from rolepermissions where 1=1 " +
            " <when test='RoleId!=null'>" +
            " AND RoleId=#{RoleId}" +
            " </when>" +
            "" +
            " ) b on a.Id=b.PermissionId" +
            " WHERE 1=1" +
            "" +
            " <when test='AppId!=null'>" +
            " AND a.AppId=#{AppId}" +
            " </when>" +
            "" +

            "" +
            "</script>")
    List<RolePermissionDto> searchRolePermission(GetRolePermissionsInput input);




    @Select("<script>" +
            "select * from rolepermissions where RoleId= #{RoleId}" +
            "" +
            "</script>")
    List<RolePermissions> getRolePermissionByRoleId(String RoleId);




    @Select("delete from rolepermissions where id=#{id}")
    int deleteRolePermission(String Id);







}
